package top.humbleyuan.dp;

/**
 * @Author HumbleYuan
 * @Date 2020/5/18 21:10
 * @Des 买卖股票时机
 */
public class LeetCode_121 {
    public static void main(String[] args) {
        int[] prices = {7,1,5,3,6,4};
        /*
          谷峰思想
          一次遍历记录下前面的最小值，遍历过程中只需将当前值与前面最小值求差取最大就行
         */
        int minPrice = prices[0];
        int maxProfit = 0;

        for (int i = 1;i < prices.length;i++) {
            minPrice = prices[i] > minPrice ? minPrice : prices[i];
            maxProfit = prices[i] - minPrice > maxProfit ? prices[i] - minPrice : maxProfit;
        }
        System.out.println(maxProfit);
    }
}
